//
//  KPThirdViewController.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/3/21.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit
import SafariServices
import Presentr
import MonkeyKing

class KPThirdViewController: KPBaseTableViewController {

    fileprivate var titles = ["加载动画", "WebView", "下拉菜单", "普通弹窗", "自定义弹出视图", "系统分享", "自定义分享","支付宝支付","微信支付","QQ 登录","微信登录","网络测试","选择图片"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.bindDefaultRefreshStyle(at: .header) { [unowned self] in
            delay(3, task: {
                self.tableView.headRefreshControl.endRefreshing(withAlertText: "刷新成功！", completion: nil)
            })
        }

        let userInfoButton = UIBarButtonItem.init(title: "UserInfo", style: .plain, target: self, action: #selector(getUserInfo))
        navigationItem.rightBarButtonItem = userInfoButton
    }

    @objc
    fileprivate func getUserInfo() {
        ///获取用户信息
        let userInfo = Preferences[.userInfo]
        KPLog(userInfo)

        //设置用户信息
        Preferences[.userInfo]?.balance = Double.random(between: 0, and: 999)
        Preferences[.userInfo]?.birthday = String.random(ofLength: 8)
        title = Preferences[.userInfo]?.balance.priceFormat
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.frame = CGRect.init(x: 0, y: kNavibarH, width: Screen.width, height: Screen.height - kNavibarH - kTabbarH)
    }

}

extension KPThirdViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 54
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell.init(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = titles[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.row {
        case 0: ///吐司提示

            navigationController?.pushViewController(KPHUDViewController())
        case 1: ///WebView 控制器,import SafariServices ,一定要用模态推送；

            let web = SFSafariViewController.init(url: "https://www.baidu.com".url)
            present(web, animated: true, completion: nil)
        case 2: ///下拉菜单，可高度自定义
            let dropMenuContro = KPDropDownMenuViewController()
            navigationController?.pushViewController(dropMenuContro)
        case 3: ///普通弹窗

            showAlert(title: "标题", message: "内容", buttonTitles: ["确定","取消"], completion: { (index) in
                KPLog(index)
            })
        case 4: ///自定义弹出视图
            let viewContro = UIViewController()
            viewContro.view.backgroundColor = UIColor.Material.red

            let presenter = Presentr.init(presentationType: .custom(width: .default, height: .custom(size: 400), center: .center)).then {
                $0.transitionType = TransitionType.coverVertical
                $0.dismissOnSwipe = true
            }
            customPresentViewController(presenter, viewController: viewContro, animated: true, completion: nil)
        case 5: ///系统分享
            showSystemShare(urlStr: "https://www.baidu.com", title: "分享标题！", description: "分享的内容！", thumbnailImage: #imageLiteral(resourceName: "默认头像"))
        case 6:///自定义分享视图
            showShareAction(urlStr: "https://www.baidu.com", title: "分享标题", description: "分享的内容", thumbnailImage: #imageLiteral(resourceName: "默认头像"))
        case 7:///支付宝支付
            payAction(urlStr: "", payType: .alipay, completionHandler: { (bool) in
                guard bool else {
                    KPLog("支付失败！")
                    return
                }
            })
        case 8:///微信支付
            payAction(urlStr: "", payType: .weiChat, completionHandler: { (bool) in
                guard bool else {
                    KPLog("支付失败！")
                    return
                }
            })
        case 9:///QQ登录
            QQLoginAction { (userInfo) in
                KPLog(userInfo)
            }
        case 10:
            WeChatLoginAction { (userInfo) in
                KPLog(userInfo)
            }
        case 11:///网络测试
            navigationController?.pushViewController(KPNetTestTableViewController())

        case 12:///选择图片
            navigationController?.pushViewController(KPChooseImageViewController())
        default:
            break
        }
    }
}
